package demo1;

import java.util.Arrays;
                                    //<>里面比较哪个类型 写哪个类
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        /*if(this.age > o.age){
            return 1;
        }else if(this.age == o.age){
            return 0;
        }else {
            return -1;
        }*/
        //return this.age - o.age;//整型比较从小到大排序
        return o.age-this.age;//降序排序
        //return this.name.compareTo(o.name);//字符串比较  在这个里面是根据名字排序
    }
}
public class Test {
    public static void mySort(Comparable[] comparable){
        //模拟冒泡排序写
        for (int i = 0; i < comparable.length-1; i++) {
            for (int j = 0; j < comparable.length-1-i; j++) {
                if(comparable[j].compareTo(comparable[j+1])>0){
                    Comparable tmp=comparable[j];
                    comparable[j]=comparable[j+1];
                    comparable[j+1]=tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0]=new Student("zhangsan",10);
        students[1]=new Student("lisi",19);
        students[2]=new Student("abc",8);
        System.out.println("排序前"+ Arrays.toString(students));

        NameComparator nameComparator = new NameComparator();
        //mySort(students);
        Arrays.sort(students,nameComparator);
        System.out.println("排序后"+Arrays.toString(students));
    }
    public static void main2(String[] args) {
        //多组数据怎么比?
        Student[] students = new Student[3];
        students[0]=new Student("zhangsan",10);
        students[1]=new Student("lisi",19);
        students[2]=new Student("abc",8);
        System.out.println("排序前"+ Arrays.toString(students));
        mySort(students);
        System.out.println("排序后"+Arrays.toString(students));
    }
    public static void main1(String[] args) {
        Student student1 = new Student("zhangsan",10);

        Student student2 = new Student("lisi",19);

        System.out.println(student1.compareTo(student2));
    }
}


